const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    more: true,
    moveData1: null,
    moveData2: null,
    inputVal: "",
    inputVal2: "搜索",
    openUrl: ['/pages/chuku/newChuku/newChuku', '1', '2'],
    clientList: [],
    page: 1,
    key: "",
    isHideLoadMore: true,
    chukupage:false
  },
  moreClick: function () {
    console.log(this.data.more)
    var animation1 = wx.createAnimation({
      duration: 3000,
      delay: 0,
      timingFunction: "ease",
    })
    var animation2 = wx.createAnimation({
      duration: 3000,
      delay: 0,
      timingFunction: "ease",
    })
    if (this.data.more) {
      animation1.translate(-55, 0).step({ duration: 1000 })
      animation2.translate(0, -55).step({ duration: 1000 })
    } else {
      animation1.translate(0, 0).step({ duration: 1000 })
      animation2.translate(0, 0).step({ duration: 1000 })
    }
    this.setData({
      more: !this.data.more,
      moveData1: animation1.export(),
      moveData2: animation2.export()
    })
  },
  showInput: function () {
    this.setData({
      inputShowed: true
    });
  },
  hideInput: function () {
    this.setData({
      inputShowed: false,
      inputVal: "",
      inputVal2: "搜索"
    });
  },
  searchBtn: function () {
    this.setData({
      inputShowed: false
    });
  },
  clearInput: function () {
    this.setData({
      inputVal: "",
      inputVal2: "搜索"
    });
  },
  inputTyping: function (e) {
    this.setData({
      inputVal: e.detail.value,
      inputVal2: e.detail.value == "" ? "搜索" : e.detail.value
    });
  },
  open: function (e) {
    var id = e.currentTarget.dataset.id
    var that = this
    wx.showActionSheet({
      itemList: ['出库', '预览', '修改'],
      success: function (res) {
        console.log(res)
        if (!res.cancel) {
          if (res.tapIndex == 0) {
            if(that.data.chukupage){
              let pages = getCurrentPages(); //获取当前页面js里面的pages里的所有信息。
              let prevPage = pages[pages.length - 2];
              var obj = e.currentTarget.dataset.item
              prevPage.setData({
                CustomerId: id,
                CustomerName: obj
              })
              wx.navigateBack({
                delta: 1
              })
            }else{
              wx.navigateTo({
                url: '/pages/chuku/newChuku/newChuku?id=' + id
              })
            }
          } else if (res.tapIndex == 1) {
            wx.navigateTo({
              url: './addClient/addClient?id=' + id
            })
          } else if (res.tapIndex == 2) {
            wx.navigateTo({
              url: './addClient/addClient?id=' + id
            })
          }
        }
      }
    });
  },
  getList: function () {
    console.log(1)
    var that = this
    app.req({
      url: 'User/Customer',
      data: {
        pageindex: this.data.page,
        pagesize: 15,
        key: this.data.key
      },
      success: function (res) {
        if (res.data.Result.length > 0) {
          if (that.data.page == 1) {
            that.setData({
              clientList: res.data.Result
            })
          } else if (that.data.page > 1) {
            that.setData({
              clientList: that.data.clientList.concat(res.data.Result)
            })
          }
        }
        setTimeout(function () {
          wx.stopPullDownRefresh()
        }, 1000)
      },
    })
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    if(options.type==4){
      this.setData({
        chukupage: true
      })
    }
    
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    
  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {
    wx.startPullDownRefresh({

    })
    
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
    
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
    
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    this.setData({
      page: 1,
      key: ''
    })
    this.getList()
    
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    console.log('加载更多')
    this.setData({
      isHideLoadMore: false,
    })
    this.setData({
      page: this.data.page + 1
    })
    this.getList()
    setTimeout(() => {
      this.setData({
        isHideLoadMore: true,
      })
    }, 1000)
    
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
    
  }
})